package com.person.leetcode.editor.cn;

/**
 * 543.二叉树的直径
 * 2022-10-12 15:56:39
 **/
public class Sequence_0543_DiameterOfBinaryTree {
    public static void main(String[] args) {
        Solution solution = new Sequence_0543_DiameterOfBinaryTree().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        int ans = 0;

        //获取最深的左右子树
        public int diameterOfBinaryTree(TreeNode root) {

            depth(root);
            return ans-1;
        }

        private int depth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int l = depth(root.left);
            int r = depth(root.right);
            ans = Math.max(ans,l+r+1);
            return Math.max(l+1,r+1);
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}